﻿#region | Copyright © 2556 |

/*
##########################################################################
	Author By : Siriwat Suananpornpanit
	DateTime : 2556/08/30
##########################################################################
*/

#endregion

#region | Imports |

using System;
using System.Collections.Generic;
using DataAccess;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;
using Web.Business;
using Web.Core;

#endregion

namespace Web.Pages
{
    public partial class FunctionApplication : System.Web.UI.Page
    {
        #region | Event Paging |

        #region | Const |

        private DataAccessClient dal = new DataAccessClient(DATABASE_PROXY.AUTH);

        private p_USER_PROFILE _userPartial;//= Core.SessionPool.GetInstance().GetSession();

        private Int32 idx = 1;

        private string _err;

        #endregion

        #region | InitAccess |

        public void InitAccess()
        {
            // Init Access
            AccessToolbar.Resource = "Resource";
            //
            AccessToolbar.AddText = "Add";
            AccessToolbar.EditText = "Edit";
            AccessToolbar.CopyText = "Copy";
            AccessToolbar.DeleteText = "Delete";
            AccessToolbar.SearchText = "Search";
            //
            AccessToolbar.SaveVisible = false;
            AccessToolbar.CancelVisible = false;
            AccessToolbar.AddClick += OnAddClick;
            AccessToolbar.EditClick += OnEditClick;
            AccessToolbar.CopyClick += OnCopyClick;
            AccessToolbar.DeleteClick += OnDeleteClick;
            AccessToolbar.SearchClick += OnSearchClick;
        }

        void OnSearchClick(object sender, EventArgs e)
        {
            // GridView Reload 
            OnReload();
        }

        void OnDeleteClick(object sender, EventArgs e)
        {
            // Response.Write("Delete");
        }

        private void OnCopyClick(object sender, EventArgs e)
        {
            Int32 result = 0;
            string applNo = null;
            string funcNo = null;

            for (Int32 i = 0; i <= grvResult.PageCount; i += 1)
            {
                ASPxCheckBox chk =
                    grvResult.FindRowCellTemplateControl(i, grvResult.Columns["Check"] as GridViewDataColumn,
                        "chk") as ASPxCheckBox;

                if (chk != null)
                    if (chk.Checked)
                    {
                        ASPxLabel lbApplNo =
                            grvResult.FindRowCellTemplateControl(i, grvResult.Columns["Application"] as GridViewDataColumn,
                                "lbApplNo") as ASPxLabel;

                        ASPxLabel lbFuncNo =
                            grvResult.FindRowCellTemplateControl(i, grvResult.Columns["Function"] as GridViewDataColumn,
                                "lbFuncNo") as ASPxLabel;

                        if (lbApplNo != null)
                            applNo = lbApplNo.Text;

                        if (lbFuncNo != null)
                            funcNo = lbFuncNo.Text;

                        result += 1;
                    }

            }

            if (result == 0)
                OnToggleModal(
                            modal: ModalFlag.Hide,
                            messageBox: MessageFlag.Wraning,
                            massage: ResourceTools.GetResource(
                                                            resourceKey: "SpecifyIsOneRec"));

            else if (result > 1)
                OnToggleModal(
                           modal: ModalFlag.Hide,
                           messageBox: MessageFlag.Wraning,
                           massage: ResourceTools.GetResource(
                                                            resourceKey: "SpecifyIsLastOneRec"));

            else
            {
                // Show Modal
                pcModal.HeaderText = ResourceTools.GetResource(
                                                            resourceKey: "FunctionApplicationAdd");
                FunctionApplicationModal.OnLoad(
                                       flag: AccessFlag.Copy,
                                       applNo: applNo,
                                       funcNo: funcNo,
                                       headerText: ResourceTools.GetResource(
                                                                        resourceKey: "FunctionApplicationAdd"),
                                       companyCode: _userPartial.COMP_CODE);
            }

        }

        private void OnEditClick(object sender, EventArgs e)
        {
            Int32 result = 0;
            string applNo = string.Empty;
            string funcNo = string.Empty;

            for (Int32 i = 0; i <= grvResult.PageCount; i += 1)
            {
                ASPxCheckBox chk =
                    grvResult.FindRowCellTemplateControl(i, grvResult.Columns["Check"] as GridViewDataColumn,
                        "chk") as ASPxCheckBox;

                if (chk != null)
                    if (chk.Checked)
                    {
                        ASPxLabel lbApplNo =
                            grvResult.FindRowCellTemplateControl(i, grvResult.Columns["Application"] as GridViewDataColumn,
                                "lbApplNo") as ASPxLabel;

                        ASPxLabel lbFuncNo =
                            grvResult.FindRowCellTemplateControl(i, grvResult.Columns["Function"] as GridViewDataColumn,
                                "lbFuncNo") as ASPxLabel;

                        if(lbApplNo != null)
                            applNo = lbApplNo.Text;

                        if(lbFuncNo!=null)
                            funcNo = lbFuncNo.Text;

                        result += 1;
                    }
            }

            if (result == 0)
                OnToggleModal(
                          modal: ModalFlag.Hide,
                          messageBox: MessageFlag.Wraning,
                          massage: ResourceTools.GetResource(
                                                            resourceKey: "SpecifyIsOneRec"));

            else if (result > 1)
                OnToggleModal(
                            modal: ModalFlag.Hide,
                            messageBox: MessageFlag.Wraning,
                            massage: ResourceTools.GetResource(
                                                            resourceKey: "SpecifyIsLastOneRec"));

            else
            {
                // Show Modal
                pcModal.HeaderText = ResourceTools.GetResource(
                                                            resourceKey: "FunctionApplicationEdit");
                FunctionApplicationModal.OnLoad(
                                       flag: AccessFlag.Edit,
                                       applNo: applNo,
                                       funcNo: funcNo,
                                       headerText: ResourceTools.GetResource(
                                                                            resourceKey: "FunctionApplicationEdit"),
                                       companyCode: _userPartial.COMP_CODE);
            }
        }

        private void OnAddClick(object sender, EventArgs e)
        {
            pcModal.HeaderText = ResourceTools.GetResource(
                                                        resourceKey: "FunctionApplicationAdd");
            FunctionApplicationModal.OnLoad(
                                   flag: AccessFlag.Add,
                                   applNo: null,
                                   funcNo: null,
                                   headerText: ResourceTools.GetResource(
                                                                        resourceKey: "FunctionApplicationAdd"),
                                   companyCode: _userPartial.COMP_CODE);
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                // Binding Event in Page and Init Session
                Binding();

                //
                if (!this.Page.IsPostBack)
                {
                    // GridView Loading 
                    OnReload();
                }

            }
            catch (System.Exception ex)
            {
                Core.Modal.Exception(
                   page: this.Page,
                   message: ex.Message);
            }

        }

        #endregion

        #region | Methods |
        
        #region | Bind |

        private void Binding()
        {
            // Partial Session
            _userPartial = SessionPool.GetInstance().GetSession(this.Page);

            // Init Controls Access
            InitAccess();

            // Init grvResult as GridView
            grvResult.HtmlRowCreated += GrvResultOnHtmlRowCreated;
            grvResult.PageIndexChanged += GrvResultOnPageIndexChanged;
            grvResult.AfterPerformCallback += GrvResultOnAfterPerformCallback;
            //

        }

        private void GrvResultOnAfterPerformCallback(object sender, ASPxGridViewAfterPerformCallbackEventArgs e)
        {
            // GridView Callback 
            OnReload();
        }

        private void GrvResultOnPageIndexChanged(object sender, EventArgs e)
        {
            // GridView Reload and Fixed Paging
            OnReload(grvResult.PageIndex);
        }

        protected void chk_Checked(object sender, EventArgs e)
        {
            GridView.HeaderChecking(
                page: this.Page,
                grvResult: grvResult);
        }

        private void GrvResultOnHtmlRowCreated(object sender, ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != GridViewRowType.Data) return;

            GridView.OnHtmlRowCreated(
                page: this.Page,
                grvResult: grvResult,
                e: e);

        }

        #endregion

        #endregion

        #region | Allow Init Permission |

        public void OnToggleModal(
            ModalFlag modal,
            MessageFlag messageBox,
            String massage, String
            headerText = null)
                            {
                                OnToggleHeaderText(headerText);
                                Core.Modal.OnToggleModal(
                                                        control: pcModal
                                                        , page: this.Page
                                                        , modalBox: modal
                                                        , messageBox: messageBox
                                                        , massage: massage
                                                        , headerText: headerText);
                            }

        public void OnToggleHeaderText(String headerText = "[?]")
        {
            pcModal.HeaderText = headerText;
        }

        public void OnReload(Int32 pageiIdx = 0)
        {
            grvResult.BeginUpdate();
            try
            {
                string optionOnMobile = (rdoAll.Checked)
                                                    ? string.Empty
                                                    : (rdoMobileOnly.Checked)
                                                        ? "Y"
                                                        : (rdoWebOnly.Checked) ? "N" : string.Empty;

                IEnumerable<DataAccess.Prop.FunctionApplication> list = FunctionApplicationBal.GetInstance()
                                                                .Select(
                                                                    page: this.Page,
                                                                    companyCode:_userPartial.COMP_CODE,
                                                                    applNo: null,
                                                                    applName: txApplicationName.Text,
                                                                    funcNo: null,
                                                                    funcName: txFunctionName.Text,
                                                                    optionMobile: optionOnMobile);

                grvResult.DataSource = list;
                grvResult.PageIndex = (pageiIdx == 0) ? grvResult.PageIndex : pageiIdx;
                grvResult.DataBind();
            }
            finally
            {
                grvResult.EndUpdate();
            }
        }

        #endregion
    }
}